/*
 * To change this license header, choose License Headers in Project Properties.
 */

package dreamwar.view;

import dreamwar.controller.ArmyJpaController;
import dreamwar.model.POJO.Army;
import dreamwar.controller.GUIController;
import dreamwar.controller.exceptions.NonexistentEntityException;
import dreamwar.model.MyEntityManagerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.observablecollections.ObservableCollections;

/**
 *
 * @author Berto_Romero
 */
public class ArmyListPanel extends javax.swing.JPanel {

    /**
     * Creates new form NewArmyJPanel
     */
    public ArmyListPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : MyEntityManagerFactory.getInstance().getEntityManager();
        armyQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT a FROM Army a");
        armyList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : ObservableCollections.observableList(armyQuery.getResultList());
        armyListScrollPane = new javax.swing.JScrollPane();
        armyListList = new javax.swing.JList();
        keypadPanel = new javax.swing.JPanel();
        createButton = new javax.swing.JButton();
        modifyArmyButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();

        setAutoscrolls(true);
        java.awt.GridBagLayout layout1 = new java.awt.GridBagLayout();
        layout1.columnWidths = new int[] {0};
        layout1.rowHeights = new int[] {0, 5, 0};
        setLayout(layout1);

        armyListScrollPane.setPreferredSize(new java.awt.Dimension(400, 200));

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, armyList, armyListList);
        jListBinding.setDetailBinding(org.jdesktop.beansbinding.ELProperty.create("${armyName}"));
        bindingGroup.addBinding(jListBinding);

        armyListScrollPane.setViewportView(armyListList);
        armyListList.setSelectedIndex(1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(25, 25, 0, 25);
        add(armyListScrollPane, gridBagConstraints);

        java.awt.GridBagLayout jPanel1Layout1 = new java.awt.GridBagLayout();
        jPanel1Layout1.columnWidths = new int[] {0, 5, 0, 5, 0};
        jPanel1Layout1.rowHeights = new int[] {0};
        keypadPanel.setLayout(jPanel1Layout1);

        createButton.setText("Crear");
        createButton.setMaximumSize(new java.awt.Dimension(75, 23));
        createButton.setMinimumSize(new java.awt.Dimension(75, 23));
        createButton.setPreferredSize(new java.awt.Dimension(75, 23));
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(createButton, gridBagConstraints);
        createButton.getAccessibleContext().setAccessibleDescription("");

        modifyArmyButton.setText("Modificar");
        modifyArmyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyArmyButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(modifyArmyButton, gridBagConstraints);

        deleteButton.setText("Borrar");
        deleteButton.setMaximumSize(new java.awt.Dimension(75, 23));
        deleteButton.setMinimumSize(new java.awt.Dimension(75, 23));
        deleteButton.setPreferredSize(new java.awt.Dimension(75, 23));
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(deleteButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 25, 45);
        add(keypadPanel, gridBagConstraints);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        GUIController.getInstance().changeMainFrameCard(GUIController.CREATE_ARMY_PANEL);
    }//GEN-LAST:event_createButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int[] selectedIndices = armyListList.getSelectedIndices();
        if (selectedIndices.length == 0) return;
        List<Army> idArmyList = new ArrayList<>();
        for (int selectedIndex : selectedIndices) {
            idArmyList.add(armyList.get(selectedIndex));
        }
        ArmyJpaController armyJpaController = new ArmyJpaController(MyEntityManagerFactory.getInstance().getEntityManagerFactory());
        for (Army army : idArmyList) {
            try {
                armyJpaController.destroy(army.getIdArmy());
                armyList.remove(army);
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(ArmyListPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        armyListList.setSelectedIndex(0);
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void modifyArmyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyArmyButtonActionPerformed
        int[] selectedIndices = armyListList.getSelectedIndices();
        if (selectedIndices.length == 0) return;
        List<Army> selectedArmyList = new ArrayList<>();
        for (int selectedIndex : selectedIndices) {
            selectedArmyList.add(armyList.get(selectedIndex));
        }
        GUIController.getInstance().prepareModifyArmyPanel(selectedArmyList);
        GUIController.getInstance().changeMainFrameCard(GUIController.MODIFY_ARMY_PANEL);
    }//GEN-LAST:event_modifyArmyButtonActionPerformed
    
    public void createArmy(Army army) {
        armyList.add(army);
    }
    
    public void createArmy(int index, Army army) {
        armyList.add(index, army);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<dreamwar.model.POJO.Army> armyList;
    private javax.swing.JList armyListList;
    private javax.swing.JScrollPane armyListScrollPane;
    private javax.persistence.Query armyQuery;
    private javax.swing.JButton createButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JPanel keypadPanel;
    private javax.swing.JButton modifyArmyButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
